﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class ClientsGroupSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AddClientsGroupsEvent(Telerik.Web.UI.GridDataItem row);
        public event AddClientsGroupsEvent OnAddClientsGroupsEvent;

        public delegate void AcceptEvent(Telerik.Web.UI.GridDataItem row);
        public event AcceptEvent OnAcceptEvent;

        //Propeties
        [Browsable(true), Category("Custom"), DefaultValue(true)]
        public bool IsAddClientsGroupsVisible { get; set; }

        public int GetClientGroupID()
        {
            if (string.IsNullOrEmpty(txtClientsGroupsID.Text))
                return -1;
            else
                return Convert.ToInt32(txtClientsGroupsID.Text);
        }

        public void ClearControls()
        {
            txtClientsGroupsID.Text = string.Empty; ;
            txtClientsGroupsName.Text = string.Empty;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnAddClientsGroups.Visible = IsAddClientsGroupsVisible;
        }

        protected void btnClientsGroupsSearch_Click(object sender, EventArgs e)
        {
            GPSTrackingDBV2.Data.ClientsGroupsQuery ClientsGroupsQuery = new GPSTrackingDBV2.Data.ClientsGroupsQuery();
            if (!string.IsNullOrEmpty(txtSearchClientsGroupsID.Text)) ClientsGroupsQuery.AppendContains(GPSTrackingDBV2.Entities.ClientsGroupsColumn.ClientGroupId, txtSearchClientsGroupsID.Text);
            if (!string.IsNullOrEmpty(txtSearchClientsGroupsName.Text)) ClientsGroupsQuery.AppendContains(GPSTrackingDBV2.Entities.ClientsGroupsColumn.Name, txtSearchClientsGroupsName.Text);


            ClientsGroupsGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.ClientsGroupsProvider.Find(ClientsGroupsQuery);
            ClientsGroupsGrid.DataBind();
        }

        protected void btnAddClientsGroups_Click(object sender, EventArgs e)
        {
            if (ClientsGroupsGrid.SelectedItems.Count > 0)
            {
                Telerik.Web.UI.GridDataItem row = ClientsGroupsGrid.SelectedItems[0] as Telerik.Web.UI.GridDataItem;
                OnAddClientsGroupsEvent(row);
            }
        }

        protected void btnClientsGroupsOK_Click(object sender, EventArgs e)
        {
            if (ClientsGroupsGrid.SelectedItems.Count > 0)
            {
                txtClientsGroupsID.Text = ClientsGroupsGrid.SelectedItems[0].Cells[3].Text;
                txtClientsGroupsName.Text = ClientsGroupsGrid.SelectedItems[0].Cells[4].Text;

                Telerik.Web.UI.GridDataItem row = ClientsGroupsGrid.SelectedItems[0] as Telerik.Web.UI.GridDataItem;

                if (OnAcceptEvent != null) OnAcceptEvent(row);
            }
        }
    }
}